// subpkg_worker/breakfast/breakfast.js
import Toast from '@vant/weapp/toast/toast';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    elderId:'1',
    drinkList:
    [
      {
        id:1,
        name:'小米粥',
        count:0
      },
      {
        id:2,
        name:'大米粥',
        count:0
      },
      {
        id:3,
        name:'八宝粥',
        count:0
      },
      {
        id:4,
        name:'南瓜粥',
        count:0
      },
      {
        id:5,
        name:'其他',
        count:0
      }
    ],
    foodList:
    [
      {
        id:1,
        name:'包子',
        count:0
      },
      {
        id:2,
        name:'油条',
        count:0
      },
      {
        id:3,
        name:'面条',
        count:0
      },
      {
        id:4,
        name:'米饭',
        count:0
      },
      {
        id:5,
        name:'其他',
        count:0
      }
    ],
    eggMilkList:
    [
      {
        id:1,
        name:'鸡蛋',
        count:0
      },
      {
        id:2,
        name:'牛奶',
        count:0
      }
    ],
    others:
    [
      {
        content:'',
        picture:''
      }
    ],
   
  },
  onChange3(e){
    var others = this.data.others
    others[0].content = e.detail.value
    this.setData({
      others:others
    })
  },
  photo(){
    var that = this
    wx.chooseMedia({
      count:1,
      mediaType:['image'],
      sourceType:['album','camera'],
      camera:'back',
      success(res){
        console.log(that)
        var others = that.data.others
        
        

        wx.uploadFile({
          url: 'https://zhixiao.site:8080/activity/upload', // 仅为示例，非真实的接口地址
          filePath: res.tempFiles[0].tempFilePath,
          name: 'file',
          formData: { user: 'test' },
          success(res1) {
            // 上传完成需要更新 fileList
            console.log(res1)
            console.log(res1.data)
            var url = res1.data.slice(32,res1.data.length-3)
            console.log(url)
            //注意client
            // others[0].picture =  url
            others[0].picture =  url.replace('/client','')
            console.log(222)
            that.setData({
              others:others
            })
          },
        });
        
      }
    })
  },
  onChange(event) {
   var index = event.currentTarget.dataset.index
   const drinkList = this.data.drinkList
   drinkList[index].count = event.detail
   this.setData({
     drinkList:drinkList
   })
  },
  onChange1(event) {
    var index = event.currentTarget.dataset.index
    const foodList = this.data.foodList
    foodList[index].count = event.detail
    this.setData({
      foodList:foodList
    })
   },
   onChange2(event) {
    console.log(event)
    var index = event.currentTarget.dataset.index
    const eggMilkList = this.data.eggMilkList
    eggMilkList[index].count = event.detail
    this.setData({
      eggMilkList:eggMilkList
    })
   },
   submit(){
     //删除count为0
     var drinkList = []
     var eggMilkList = []
     var foodList = []
     for(var i in this.data.drinkList){
       if(this.data.drinkList[i].count!=0){
         drinkList.push(this.data.drinkList[i])
       }
     }
     for(var i in this.data.eggMilkList){
      if(this.data.eggMilkList[i].count!=0){
        eggMilkList.push(this.data.eggMilkList[i])
      }
     }
     for(var i in this.data.foodList){
      if(this.data.foodList[i].count!=0){
        foodList.push(this.data.foodList[i])
      }
     }
     if(drinkList.length===0&&eggMilkList.length===0&&foodList.length===0&&this.data.others[0].content===''&&this.data.others[0].picture===''){
       Toast('请选择后再录入！')
       return
     }
     wx.request({
       url: 'https://zhixiao.site:8080/client/breakfast_add',
       data:
       {
         elderId:this.data.elderId,
         drinkList:drinkList,
         foodList:foodList,
         eggMilkList:eggMilkList,
         others:this.data.others
       },
       method:'POST',
       header: {  
        "Content-Type": "application/json"
      },  
       success:(res)=>{
         if(res.data.msg==='重复添加'){
          Toast('今日已上传！');
          return
         }
          console.log("submit success")
          console.log(res)
          Toast('上传成功');
       }
     })
     
   },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      elderId:options.id
    })
    // this.query()

  },
  query(){
    wx.request({
      url: 'https://zhixiao.site:8080/client/breakfast_query',
      data:{
        elderId:this.data.elderId
      },
      success:(res)=>{
        console.log(res)
        console.log(res.data.data.drinkList.length)
        this.setData({
          drinkList:res.data.data.drinkList.length===0?this.data.drinkList:res.data.data.drinkList,
          foodList:res.data.data.foodList.length===0?this.data.foodList:res.data.data.foodList,
          eggMilkList:res.data.data.eggMilkList.length===0?this.data.eggMilkList:res.data.data.eggMilkList,
          others:res.data.data.others.length===0?this.data.others:res.data.data.others
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})